<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\models\UserCardRecharge;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this yii\web\View
 * @var $rechargeList app\models\UserCardRecharge[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '卡充值列表';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <label for="search_username" class="sr-only">用户</label>
    <?php echo Html::textInput('search_username', Yii::$app->request->get('search_username'), ['id' => 'search_username', 'class' => 'form-control', 'placeholder' => '用户名', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <label for="search_status" class="sr-only">状态</label>
    <?php echo Html::dropDownList('search_status', Yii::$app->request->get('search_status'), KeyMap::getValues('user_card_recharge_status'), ['id' => 'search_status', 'class' => 'form-control', 'prompt' => '搜索状态']);?>
</div>
<div class="form-group">
    <label for="search_iccid" class="sr-only">ICCID</label>
    <?php echo Html::textInput('search_iccid', Yii::$app->request->get('search_iccid'), ['id'=>'search_iccid', 'class'=>'form-control', 'placeholder'=>'ICCID', 'style' => 'min-width:180px;']);?>
</div>
<div class="form-group">
    <label for="search_msisdn" class="sr-only">MSISDN</label>
    <?php echo Html::textInput('search_msisdn', Yii::$app->request->get('search_msisdn'), ['id'=>'search_msisdn', 'class'=>'form-control', 'placeholder'=>'MSISDN', 'style' => 'width:120px;']);?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th>用户</th>
        <th>ICCID/MSISDN/IMSI</th>
        <th>套餐</th>
        <th>充值时长</th>
        <th>价格</th>
        <th>金额</th>
        <th>支付</th>
        <th><abbr title="平台交易流水号&#10;&#13;第三方交易支付流水号">交易号</abbr></th>
        <th>状态</th>
        <th>创建/支付时间</th>
        <th>操作</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($rechargeList as $recharge) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $recharge->id;?>" /><span class="lbl"><?php echo $recharge->id;?></span></label></td>
            <td><?php echo Html::a(Html::encode($recharge->user->username), ['/admin/user/user-view', 'id' => $recharge->uid]);?></td>
            <td><?php echo empty($recharge->card->iccid) ? '----' : Html::encode($recharge->card->iccid);
                echo '<br />';
                echo empty($recharge->card->msisdn) ? '----' : Html::encode($recharge->card->msisdn);
                echo '<br />';
                echo empty($recharge->card->imsi) ? '----' : Html::encode($recharge->card->imsi);?></td>
            <td><?php echo Html::encode($recharge->package->name);?></td>
            <td><?php echo $recharge->term_length, KeyMap::getValue('card_term_length_unit', $recharge->term_length_unit);?></td>
            <td><?php echo $recharge->price;?></td>
            <td><?php echo $recharge->money;?></td>
            <td><?php echo KeyMap::getValue('user_card_recharge_trade_type', $recharge->trade_type);?></td>
            <td><?php echo Html::encode($recharge->trade_no), '<br />', Html::encode($recharge->trans_id);?></td>
            <td><?php echo KeyMap::getValue('user_card_recharge_status', $recharge->status);?></td>
            <td><?php echo Yii::$app->formatter->asDatetime($recharge->create_time), '<br />', Yii::$app->formatter->asDatetime($recharge->pay_time);?></td>
            <td><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-info-circle', 'href' => Url::to(['/admin/card/recharge-view', 'id' => $recharge->id]), 'btn_class' => 'btn btn-xs btn-default', 'tip' => '详情'],
                    !($recharge->status == UserCardRecharge::STATUS_WAIT_PAY) ?: ['icon' => 'fa fa-check', 'onclick' => 'setPaid(' . $recharge->id . ')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '设置已支付'],
                    !($recharge->status == UserCardRecharge::STATUS_PAID) ?: ['icon' => 'fa fa-check', 'onclick' => 'addTerm(' . $recharge->id . ')', 'btn_class' => 'btn btn-xs btn-success', 'tip' => '更新卡'],
                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script>
    /**
     * 设置已支付
     * @param id 充值编号
     */
    function setPaid(id) {
        $.getJSON('<?php echo Url::to(['/admin/card/set-recharge-paid']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('设置成功。', function () {window.location.reload();});
            }
        });
    }

    /**
     * 更新卡套餐时长
     * @param id 充值编号
     */
    function addTerm(id) {
        $.getJSON('<?php echo Url::to(['/admin/card/add-recharge-term']);?>', {id: id}, function (json) {
            if (callback(json)) {
                layer.msg('更新成功。', function () {window.location.reload();});
            }
        });
    }
</script>
